﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data;
using System.Data.SqlClient;

/// <summary>
/// Summary description for DanhMucConDAO
/// </summary>
public class DanhMucConDAO
{
	DataProvider provider;
    public DanhMucConDAO()
	{
        provider = new DataProvider();
	}

    public List<DanhMucCon> HienThiDanhMucCon(int key, int idDanhMuc)
    {
        List<DanhMucCon> list = new List<DanhMucCon>();
        try
        {
            SqlCommand cmd = new SqlCommand("sp_HienThiDanhMucCon", provider.Conn);
            cmd.Parameters.Add("@Key", SqlDbType.Int).Value = key;
            cmd.Parameters.Add("@ID", SqlDbType.Int).Value = idDanhMuc;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection.Open();

            SqlDataAdapter adapter = new SqlDataAdapter();
            adapter.SelectCommand = cmd;
            DataSet ds = new DataSet();
            adapter.Fill(ds);
            cmd.Connection.Close();

            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                DanhMucCon dto = new DanhMucCon();
                dto.IDDanhMucCon = Convert.ToInt32(ds.Tables[0].Rows[i]["IDDanhMucCon"].ToString());
                dto.IDDanhMuc = Convert.ToInt32(ds.Tables[0].Rows[i]["IDDanhMuc"].ToString());
                dto.TenDanhMucCon = ds.Tables[0].Rows[i]["TenDanhMucCon"].ToString();
                dto.NoiDung = ds.Tables[0].Rows[i]["NoiDung"].ToString();
                dto.NgayDang = Convert.ToDateTime(ds.Tables[0].Rows[i]["NgayDang"].ToString());
                dto.TenTat = ds.Tables[0].Rows[i]["TenTat"].ToString();
                list.Add(dto);
            }
        }
        catch (Exception e)
        {
            throw e;
        }
        finally
        {
            provider.Conn.Close();
        }
        return list;
    }

    public List<DanhMucCon> HienThiDanhMucTimKiem(int idDanhMuc, string tuKhoa)
    {
        List<DanhMucCon> list = new List<DanhMucCon>();
        try
        {
            SqlCommand cmd = new SqlCommand("sp_HienThiDanhMucTimKiem", provider.Conn);
            cmd.Parameters.Add("@ID", SqlDbType.Int).Value = idDanhMuc;
            cmd.Parameters.Add("@TuKhoa", SqlDbType.NVarChar).Value = tuKhoa;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection.Open();

            SqlDataAdapter adapter = new SqlDataAdapter();
            adapter.SelectCommand = cmd;
            DataSet ds = new DataSet();
            adapter.Fill(ds);
            cmd.Connection.Close();

            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                DanhMucCon dto = new DanhMucCon();
                dto.IDDanhMucCon = Convert.ToInt32(ds.Tables[0].Rows[i]["IDDanhMucCon"].ToString());
                dto.IDDanhMuc = Convert.ToInt32(ds.Tables[0].Rows[i]["IDDanhMuc"].ToString());
                dto.TenDanhMucCon = ds.Tables[0].Rows[i]["TenDanhMucCon"].ToString();
                dto.NoiDung = ds.Tables[0].Rows[i]["NoiDung"].ToString();
                dto.NgayDang = Convert.ToDateTime(ds.Tables[0].Rows[i]["NgayDang"].ToString());
                dto.TenTat = ds.Tables[0].Rows[i]["TenTat"].ToString();
                list.Add(dto);
            }
        }
        catch (Exception e)
        {
            throw e;
        }
        finally
        {
            provider.Conn.Close();
        }
        return list;
    }

    public int ThemDanhMucCon(int idDanhMuc, string tenDMCon, string noiDung, string tenTat)
    {
        int kq = 0;
        try
        {
            SqlCommand cmd = new SqlCommand("sp_ThemDanhMucCon", provider.Conn);
            cmd.Parameters.Add("@ID", SqlDbType.Int).Value = 0;
            cmd.Parameters.Add("@IDDanhMuc", SqlDbType.Int).Value = idDanhMuc;
            cmd.Parameters.Add("@TenDanhMucCon", SqlDbType.NVarChar).Value = tenDMCon;
            cmd.Parameters.Add("@NoiDung", SqlDbType.NText).Value = tenDMCon;
            cmd.Parameters.Add("@TenTat", SqlDbType.VarChar).Value = tenTat;
            
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection.Open();
            cmd.ExecuteNonQuery();
            kq = 1;
        }
        catch (Exception e)
        {
            throw e;
        }
        finally
        {
            provider.Conn.Close();
        }
        return kq;
    }

    public int CapNhatDanhMucCon(int id, int idDanhMuc, string tenDMCon, string noiDung, string tenTat)
    {
        int kq = 0;
        try
        {
            SqlCommand cmd = new SqlCommand("sp_ThemDanhMucCon", provider.Conn);
            cmd.Parameters.Add("@ID", SqlDbType.Int).Value = id;
            cmd.Parameters.Add("@IDDanhMuc", SqlDbType.Int).Value = idDanhMuc;
            cmd.Parameters.Add("@TenDanhMucCon", SqlDbType.NVarChar).Value = tenDMCon;
            cmd.Parameters.Add("@NoiDung", SqlDbType.NText).Value = noiDung;
            cmd.Parameters.Add("@TenTat", SqlDbType.VarChar).Value = tenTat;

            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection.Open();
            cmd.ExecuteNonQuery();
            kq = 1;
        }
        catch (Exception e)
        {
            throw e;
        }
        finally
        {
            provider.Conn.Close();
        }
        return kq;
    }


    public int XoaDanhMucCon(int id)
    {
        int kq = 0;
        try
        {
            SqlCommand cmd = new SqlCommand("sp_XoaDanhMucCon", provider.Conn);
            cmd.Parameters.Add("@ID", SqlDbType.Int).Value = id;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection.Open();
            cmd.ExecuteNonQuery();
            kq = 1;
        }
        catch (Exception e)
        {
            throw e;
        }
        finally
        {
            provider.Conn.Close();
        }
        return kq;
    }
}